package com.rabbit.demo.direct;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName testController
 * @Description TODO
 * @Author ylr
 * @Date 2021/7/19 16:00
 * @Version 1.0
 */


@RestController
@RequestMapping("api")
@Slf4j
@Api(value = "testController",tags = "test")
public class testController {

    @Resource
    Producer producer;

    @Resource
    AutoRouteService autoRouteService;


    /**
     * 生产者控制器
     * <br/>
     * @param exchange	交换机
 * @param routingKey	路由key
 * @param msg	消息内容
 * @param correlationData 消息唯一id
 * @param priority 优先级
 * @param expiration 过期时间
     * @return java.lang.Object
     * @author ylr
     * @date 2021/7/20 11:01
     **/
    @ApiOperation(value = "生产者生成消息",tags = "test",notes = "get请求",nickname = "别名",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "exchange",
                    value = "交换机", required = true, defaultValue = "CDM-ONE"),
            @ApiImplicitParam(paramType = "path", name = "routingKey",
                    value = "路由key", required = true, defaultValue = "CDM-ONE-AGENT-GENERAL"),
            @ApiImplicitParam(paramType = "path", name = "msg",
                    value = "消息内容", required = true, defaultValue = "HELLO WORLD"),
            @ApiImplicitParam(paramType = "path", name = "correlationData",
                    value = "消息唯一id", required = true, defaultValue = "KEY-123"),
            @ApiImplicitParam(paramType = "path",name = "priority",
                    value = "优先级", required = true,dataType = "int"),
            @ApiImplicitParam(paramType = "path", name = "expiration",
                    value = "过期时间", required = true, defaultValue = "60000")
    })
    @GetMapping("producer/{exchange}/{routingKey}/{msg}/{correlationData}/{priority}/{expiration}")
    public String ProducerController(@PathVariable String exchange,
                                     @PathVariable String routingKey,
                                     @PathVariable String msg,
                                     @PathVariable String correlationData,
                                     @PathVariable Integer priority,
                                     @PathVariable String expiration){
        log.info("生产者生成消息开始！");

        CorrelationData cData = null;
        try {
            cData = new CorrelationData(correlationData);
//            for (int i = 0; i < 100000; i++) {
                producer.convertAndSend(exchange,routingKey,msg,cData,priority,expiration);
//            }


            log.info("生产者生成消息成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new String("生产者生成消息失败！");
        }

        return new String("生产者生成消息成功！");
    }


    @ApiOperation(value = "消费者根据id取消息",tags = "test",notes = "get请求",nickname = "别名",httpMethod =
            "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id",
                    value = "id", required = true, defaultValue = "test")
    })
    @GetMapping("consumer/{id}")
    public String ConsumerController(@PathVariable String id){
        String msg = autoRouteService.getMsg(id);
        if (msg==null||msg.isEmpty()){
            return "cache中取不到值！";
        }
        return msg;
    }



}
